package com.wilson.vod.Service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
//import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.wilson.servicebase.config.exceptionHandler.GuliException;
import com.wilson.vod.Service.VodService;
import com.wilson.vod.utils.ConstantPropertiesUtils;
import com.wilson.vod.utils.InitVodObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {

    //上传视频到阿里云
    @Override
    public String uploadVideoAliyun(MultipartFile file) {
        try {
            //accessKeyId, accessKeySecret
            //fileName:上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title:上传之后显示的名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream:上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtils.ACCESS_KEY_ID,
                    ConstantPropertiesUtils.ACCESS_KEY_SECRET,
                    title, fileName, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                if(StringUtils.isEmpty(videoId)){
                    throw new GuliException(20001, errorMessage);
                }
            }
            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001, "vod 服务上传失败");
        }
    }

    //根据视频id删除阿里云视频
    @Override
    public void removeVideo(String videoId) {
        try{
            //初始化对象
            DefaultAcsClient client = InitVodObject.initVodClient(
                    ConstantPropertiesUtils.ACCESS_KEY_ID,
                    ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(videoId);
            //调用初始化对象方法 实现删除
            DeleteVideoResponse response = client.getAcsResponse(request);
//            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (ClientException e){
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }

    //根据id删除多个阿里云视频
    @Override
    public void removeBatchVideo(List<String> videoIdList) {

        try{
            //初始化对象
            DefaultAcsClient client = InitVodObject.initVodClient(
                    ConstantPropertiesUtils.ACCESS_KEY_ID,
                    ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //将集合转换为1,2,3格式
            //org.apache.commons.lang.StringUtils;
            String videoIds = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");
            //向request设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象方法 实现删除
            DeleteVideoResponse response = client.getAcsResponse(request);

        }catch (ClientException e){
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }

    //根据视频id获取视频凭证
    @Override
    public String getVideoPlayAuth(String id) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodObject.initVodClient(
                    ConstantPropertiesUtils.ACCESS_KEY_ID,
                    ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建获取视频地址request对象和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象设置视频id值
            request.setVideoId(id);

            //注意：播放凭证有过期时间，默认值：100秒 。取值范围：100~3000。
            //设置播放凭证的有效期200s
            request.setAuthInfoTimeout(200L);

            //响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);

            //获取视频播放凭证
            return response.getPlayAuth();

        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取视频凭证失败");
        }
    }


}
